//
//  HomeSearchHotView.m
//  CarUser
//
//  Created by 王森林 on 2018/12/6.
//  Copyright © 2018年 王森林. All rights reserved.
//

#import "HomeSearchHotView.h"
#import "ChooseResultViewController.h"
@interface keyBtn : UIButton

- (instancetype)initWithFrame:(CGRect)frame WithTitle:(NSString *)btnTitle;

- (void)updateFrameWithX:(float)x withY:(float)y;

@end

@implementation keyBtn

- (instancetype)initWithFrame:(CGRect)frame WithTitle:(NSString *)btnTitle
{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleLabel.font = FONT_SIZE(12);
        [self setTitle:btnTitle forState:UIControlStateNormal];
        [self setTitleColor:Common_TextLowColor forState:UIControlStateNormal];
        self.layer.borderWidth = 1;
        self.layer.borderColor = Common_Head_Color.CGColor;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5;
        
        CGRect rect = self.frame;
        rect.size.width = [self getBtnWith:btnTitle];
        self.frame = rect;
        
    }
    return self;
}

-  (float)getBtnWith:(NSString *)btnTitle
{
    
    NSDictionary *attr=@{NSFontAttributeName:self.titleLabel.font};
    CGSize titleSize = [btnTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, self.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context: nil].size;
    return titleSize.width+20;
    
}


- (void)updateFrameWithX:(float)x withY:(float)y withBtn:(NSString *)btnTitle
{
    CGRect rect = self.frame;
    rect.origin.x = x;
    rect.origin.y = y;
    rect.size.width = [self getBtnWith:btnTitle];
    self.frame = rect;
    
}



@end



@interface HomeSearchHotView()
@property (nonatomic, strong) NSArray *dataAry;
@property (nonatomic, strong) UILabel *titleLabel;
@end

#define BTNTAG 10000
@implementation HomeSearchHotView

- (instancetype)initWithFrame:(CGRect)frame WithData:(nonnull NSArray *)data
{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataAry = [[NSArray alloc] initWithArray:data];
        [self createUI];
    }
    
    return self;
}

- (void)createUI
{
    [self removeAllSubviews];
    [self addSubview:self.titleLabel];
    [self createConstraint];
    
    __block float x = 15;
    __block float y = 43;
    [self.dataAry enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *dict = self.dataAry[idx];
        NSString *title = [dict objectForKey:@"NAME00"];
        keyBtn *btn = [[keyBtn alloc] initWithFrame:CGRectMake(x, y, 10, 30) WithTitle:title];
        [btn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = BTNTAG + idx;
        [self addSubview:btn];
        x = x + [btn getBtnWith:title];
        if (x > BOUNDWIDTH) {
            x  = 15;
            y =  y + 40;
            [btn updateFrameWithX:x withY:y withBtn:title];
            x = x+[btn getBtnWith:title] + 10;
        }  else {
            x = x+10;
        }
    }];
  
    
    
}

- (void)createConstraint
{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(15);
        make.top.mas_equalTo(self.mas_top).offset(15);
        make.left.equalTo(self.mas_right).offset(-15);
        make.height.mas_equalTo(13);
    }];
    
}

//- (UIViewController *)findViewController
//{
//    id target=self;
//    while (target) {
//        target = ((UIResponder *)target).nextResponder;
//        if ([target isKindOfClass:[UIViewController class]]) {
//            break;
//        }
//    }
//    return target;
//}

- (void)handleBtnAction:(id)sender
{
    UIButton *btn = sender;
    NSInteger tag  = btn.tag - BTNTAG;
//    Car_CarDetailViewController *push = [[Car_CarDetailViewController alloc]init];
//    NSDictionary  *dict = self.dataAry[tag];
//    push.SERIES_ID = [dict objectForKey:@"SALECAR_ID"];
//   [[self findViewController].navigationController pushViewController:push animated:YES];
    NSDictionary  *dict = self.dataAry[tag];
    ChooseResultViewController  *controller  = [[ChooseResultViewController alloc] init];
    controller.type = @"2";
      controller.pushType = 1;
    controller.SERIES_ID = [dict objectForKey:@"SERIES_ID"];
    [[self  viewController].navigationController pushViewController:controller animated:YES];
    
    
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = Common_Text_TextfieldColor;
        _titleLabel.font = FONT_SIZE(12);
    }
    return _titleLabel;
    
    
}

- (void)setTitleName:(NSString *)titleName
{
    if ([CommonMethod isBlankString:titleName]) {
        self.titleLabel.text = @"热门搜索"; //默认文字
    } else {
        
        self.titleLabel.text  = titleName;
    }
    
    
}

@end
